package org.assistant.sample.evolution;

import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

import org.assistant.worker.Extractor_WeatherNW;
import org.assistant.worker.RDBCurator_FusionTable;

public class WintorEvolution extends Evolution {
	private int frequency;
	private int duration;
	private int condition;
	
	public WintorEvolution(int frequency, int duration, int condition){
		this.frequency = frequency; // precision: 1 second
		this.duration = duration;  // precision: 1 day
		this.condition = condition; // 0:track everything, 1:
	}
	
	public WintorEvolution(){
		this.frequency = 1 * 60 * 60; // 1 hour = 3600 seconds
		this.duration = 1 * 365 ; // 1 year = 365 days
		this.condition = 0; // record everything
	}
	
	public void startTrack(){
		String evolution_id = UUID.randomUUID().toString();
		Calendar start = Calendar.getInstance();
		Calendar end = Calendar.getInstance();
		System.out.println();
		Extractor_WeatherNW weatherNW = new Extractor_WeatherNW("http://www.theweathernetwork.com/weather/caon1761");
		Wintor wintor;
		RDBCurator_FusionTable fusionTable =  new RDBCurator_FusionTable("leijiangh@gmail.com", "a3RMd54l", "671581"); 

		while ((end.getTimeInMillis() - start.getTimeInMillis()) < duration * 24 * 60 * 60 * 1000){ 
			wintor = weatherNW.getWintor();
			fusionTable.insertWintor(evolution_id, wintor.getTimestampDateStr(), wintor.getTimestampTimeStr(), wintor.getDirection(), wintor.getSpeed(), wintor.getSpeedUnit());
			System.out.println(wintor.toString());
			end = Calendar.getInstance();
			try {
				Thread.sleep(frequency * 1000); 
			} catch (InterruptedException e) {
				System.out.println(e);
			}
		}
	}
	
	public static void main(String args[]){
		// check every 60 second for 1 day recording everything
		WintorEvolution wintorEvolution = new WintorEvolution(60, 1, 0); 
		wintorEvolution.startTrack();
	}

}
